package com.example.poemtest.find.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.poemtest.Activity.BottomNavigationViewActivity;
import com.example.poemtest.R;
import com.example.poemtest.find.adapter.NineGridAdapter;
import com.example.poemtest.find.bean.CircleBean;
import com.example.poemtest.find.spannable.OnAddPicturesListener;
import com.example.poemtest.find.view.NineGridView;
import com.example.poemtest.utils.Constant;
import com.example.poemtest.utils.FullScreenUtil;
import com.giftedcat.picture.lib.selector.MultiImageSelector;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 发布动态
 */
public class CreatePostActivity extends AppCompatActivity {

    private static final int REQUEST_IMAGE = 2;
    private int maxNum = 9;

    public Context context;
    public Activity instans;
    Unbinder unbinder;

    @BindView(R.id.rv_images)
    RecyclerView rvImages;
    private Button post;
    NineGridAdapter adapter;
    private EditText edConent;
    private String conent;
    private ImageView back;
    List<String> mSelectList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FullScreenUtil.getInstance().fullScreen(this,true);
        setContentView(R.layout.activity_create_post);

        context = this;
        instans = this;
        unbinder = ButterKnife.bind(this);

        mSelectList = new ArrayList<>();
        initView();
    }

    private void initView() {
        edConent = findViewById(R.id.edt_content);
        rvImages.setLayoutManager(new GridLayoutManager(this, 3));
        adapter = new NineGridAdapter(CreatePostActivity.this, mSelectList, rvImages);
//        Constant.friends.add(friendDongTai);
        adapter.setMaxSize(maxNum);
        rvImages.setAdapter(adapter);
        adapter.setOnAddPicturesListener(new OnAddPicturesListener() {
            @Override
            public void onAdd() {
                pickImage();
            }
        });
        post = findViewById(R.id.btn_publish);
        post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                conent = edConent.getText().toString();
                //封装成一个类，加入到动态列表里面
                CircleBean.DataBean bean = new CircleBean.DataBean();
                bean.setType(2);
                bean.setContent(edConent.getText().toString());
                bean.setHead_img(Constant.userPhoto);
                bean.setUser_name(Constant.userName);
                bean.setCreateon("刚刚");
//                Constant.postList.add(0,bean);
                Intent intent = new Intent(CreatePostActivity.this, BottomNavigationViewActivity.class);
                intent.putExtra("id",3);
                intent.putExtra("bean",bean);
                startActivity(intent);
            }
        });
        back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CreatePostActivity.this,BottomNavigationViewActivity.class);
                intent.putExtra("id",3);
                startActivity(intent);
            }
        });
    }

    /**
     * 选择需添加的图片
     */
    private void pickImage() {
        MultiImageSelector selector = MultiImageSelector.create(context);
        selector.showCamera(true);
        selector.count(maxNum);
        selector.multi();
        selector.origin(mSelectList);
        selector.start(instans, REQUEST_IMAGE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE) {
            if (resultCode == RESULT_OK) {
                List<String> select = data.getStringArrayListExtra(MultiImageSelector.EXTRA_RESULT);
                mSelectList.clear();
                mSelectList.addAll(select);
                adapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

}